//
//  ViewController.m
//  DynamicTableViewTest
//
//  Created by SssssQG on 2020/7/14.
//  Copyright © 2020 YYL. All rights reserved.
//

#import "ViewController.h"
//#import "TableViewCell_0.h"
#import "YYTableViewCell_0.h"
#import "YYTableViewCell_1.h"
#import "YYTableViewCell_2.h"
#import "YYTableViewCell_3.h"
#import "DynamicViewModel.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIView *navView;

@property (nonatomic, strong) DynamicViewModel *viewModel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupUI];
    
    [self viewAndData];
}

- (void)setupUI {
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.navView];
    
}

- (void)viewAndData {
    self.viewModel.type = 0;
    [self.tableView reloadData];
}

- (UIView *)navView {
    if (!_navView) {
        _navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 64)];
        _navView.backgroundColor = [UIColor whiteColor];
        
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightBtn setTitle:@"变化" forState:UIControlStateNormal];
        [rightBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        rightBtn.frame = CGRectMake(self.view.bounds.size.width - 15 - 44, 20, 44, 44);
        [rightBtn addTarget:self action:@selector(rightBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [_navView addSubview:rightBtn];
    }
    return _navView;
}

- (void)rightBtnClick {
    
}

- (DynamicViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [[DynamicViewModel alloc]init];
    }
    return _viewModel;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.tableFooterView = [UIView new];
        _tableView.estimatedRowHeight = 0.001;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"YYTableViewCell_0" bundle:nil] forCellReuseIdentifier:@"YYTableViewCell_0"];
        [_tableView registerNib:[UINib nibWithNibName:@"YYTableViewCell_1" bundle:nil] forCellReuseIdentifier:@"YYTableViewCell_1"];
        [_tableView registerNib:[UINib nibWithNibName:@"YYTableViewCell_2" bundle:nil] forCellReuseIdentifier:@"YYTableViewCell_2"];
        [_tableView registerNib:[UINib nibWithNibName:@"YYTableViewCell_3" bundle:nil] forCellReuseIdentifier:@"YYTableViewCell_3"];
//        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(tableviewRefresh)];
    }
    return _tableView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.viewModel.sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.viewModel.sections[section].rows.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    return self.viewModel.sections[section].sectionHeaderView?:[UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return self.viewModel.sections[section].sectionHeaderViewHeight?:0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return self.viewModel.sections[section].sectionFooterView?:[UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return self.viewModel.sections[section].sectionFooterViewHeight?:0.01;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *cellClassNameStr = self.viewModel.sections[indexPath.section].rows[indexPath.row].cellClassName;
    
    if ([cellClassNameStr isEqualToString:@"YYTableViewCell_0"]) {
        YYTableViewCell_0 *cell = [tableView dequeueReusableCellWithIdentifier:@"YYTableViewCell_0" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if ([cellClassNameStr isEqualToString:@"YYTableViewCell_1"]) {
        YYTableViewCell_1 *cell = [tableView dequeueReusableCellWithIdentifier:@"YYTableViewCell_1" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if ([cellClassNameStr isEqualToString:@"YYTableViewCell_2"]) {
        YYTableViewCell_2 *cell = [tableView dequeueReusableCellWithIdentifier:@"YYTableViewCell_2" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if ([cellClassNameStr isEqualToString:@"YYTableViewCell_3"]) {
        YYTableViewCell_3 *cell = [tableView dequeueReusableCellWithIdentifier:@"YYTableViewCell_3" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else {
        return [UITableViewCell new];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (void)tableviewRefresh {
    
}


@end
